import QtQuick 2.15
import QtQuick.Controls 2.15

Item {
    id: root

    // 属性定义
    property string text: "Button"
    property string type: "default"  // default, primary, success, warning, danger, info
    property string size: "medium"   // medium, small, mini
    property bool disabled: false
    property bool loading: false
    property string icon: ""
    property bool plain: false
    property bool round: false
    property bool circle: false

    // 事件定义
    signal clicked()

    // 尺寸计算
    property int buttonWidth: {
        if (size === "medium") return 80;
        else if (size === "small") return 70;
        else if (size === "mini") return 60;
        else return 80;
    }

    property int buttonHeight: {
        if (size === "medium") return 40;
        else if (size === "small") return 32;
        else if (size === "mini") return 28;
        else return 40;
    }

    // 设置组件大小
    width: buttonWidth
    height: buttonHeight

    // 样式映射
    property var styleMap: ({
        "default": {
            "backgroundColor": "#FFFFFF",
            "borderColor": "#DCDFE6",
            "color": "#606266",
            "hoverBackgroundColor": "#ecf5ff",
            "hoverBorderColor": "#c6e2ff",
            "activeBackgroundColor": "#FFFFFF",
            "activeBorderColor": "#3a8ee6"
        },
        "primary": {
            "backgroundColor": "#409EFF",
            "borderColor": "#409EFF",
            "color": "#FFFFFF",
            "hoverBackgroundColor": "#66b1ff",
            "hoverBorderColor": "#66b1ff",
            "activeBackgroundColor": "#3a8ee6",
            "activeBorderColor": "#3a8ee6"
        },
        "success": {
            "backgroundColor": "#67C23A",
            "borderColor": "#67C23A",
            "color": "#FFFFFF",
            "hoverBackgroundColor": "#85ce61",
            "hoverBorderColor": "#85ce61",
            "activeBackgroundColor": "#5daf34",
            "activeBorderColor": "#5daf34"
        },
        "warning": {
            "backgroundColor": "#E6A23C",
            "borderColor": "#E6A23C",
            "color": "#FFFFFF",
            "hoverBackgroundColor": "#ebb563",
            "hoverBorderColor": "#ebb563",
            "activeBackgroundColor": "#cf9236",
            "activeBorderColor": "#cf9236"
        },
        "danger": {
            "backgroundColor": "#F56C6C",
            "borderColor": "#F56C6C",
            "color": "#FFFFFF",
            "hoverBackgroundColor": "#f78989",
            "hoverBorderColor": "#f78989",
            "activeBackgroundColor": "#dd6161",
            "activeBorderColor": "#dd6161"
        },
        "info": {
            "backgroundColor": "#909399",
            "borderColor": "#909399",
            "color": "#FFFFFF",
            "hoverBackgroundColor": "#a6a9ad",
            "hoverBorderColor": "#a6a9ad",
            "activeBackgroundColor": "#82848a",
            "activeBorderColor": "#82848a"
        }
    })

    // 根据 plain 属性调整样式
    property var currentStyle: {
        var style = styleMap[type] || styleMap["default"];
        if (plain) {
            return {
                "backgroundColor": "#FFFFFF",
                "borderColor": style["borderColor"],
                "color": style["borderColor"],
                "hoverBackgroundColor": style["backgroundColor"],
                "hoverBorderColor": style["borderColor"],
                "activeBackgroundColor": style["backgroundColor"],
                "activeBorderColor": style["activeBorderColor"]
            }
        }
        return style;
    }

    // 鼠标区域
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        enabled: !disabled && !loading
        hoverEnabled: true
        onClicked: {
            if (!disabled && !loading) {
                root.clicked();
            }
        }
    }

    // 背景矩形
    Rectangle {
        id: background
        anchors.fill: parent
        radius: round || circle ? height / 2 : 4
        color: {
            if (disabled || loading) return "#f5f7fa";
            if (mouseArea.pressed) return currentStyle["activeBackgroundColor"];
            if (mouseArea.containsMouse) return currentStyle["hoverBackgroundColor"];
            return currentStyle["backgroundColor"];
        }
        border.width: 1
        border.color: {
            if (disabled || loading) return "#e4e7ed";
            if (mouseArea.pressed) return currentStyle["activeBorderColor"];
            if (mouseArea.containsMouse) return currentStyle["hoverBorderColor"];
            return currentStyle["borderColor"];
        }

        // 加载指示器
        Rectangle {
            id: loadingIndicator
            anchors.centerIn: parent
            width: 16
            height: 16
            radius: 8
            color: "transparent"
            border.color: currentStyle["color"]
            border.width: 2
            visible: loading
            RotationAnimator on rotation {
                running: loading
                from: 0
                to: 360
                duration: 1000
                loops: Animation.Infinite
            }
        }

        // 按钮文本
        Text {
            id: buttonText
            anchors.centerIn: parent
            text: root.text
            color: {
                if (disabled || loading) return "#c0c4cc";
                return currentStyle["color"];
            }
            font.pixelSize: {
                if (size === "medium") return 14;
                else if (size === "small") return 12;
                else if (size === "mini") return 12;
                else return 14;
            }
            visible: !loading
        }
    }
}